package cn.ljn.org.controller;


import cn.ljn.basic.util.AjaxResult;
import cn.ljn.basic.util.ExcelUtil;
import cn.ljn.basic.util.PageList;
import cn.ljn.org.domain.Shop;
import cn.ljn.org.dto.ShopAuditDto;
import cn.ljn.org.dto.ShopSettlementDto;
import cn.ljn.org.query.ShopQuery;
import cn.ljn.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    //查询所有
    @GetMapping()
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }

    // 添加修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.add(shop);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    @DeleteMapping("/{id}")
    // 删除
    public AjaxResult delete(@PathVariable("id") Long id) {
        shopService.delete(id);
        return AjaxResult.me().success();
    }

    // 分页查询
    @PostMapping("/list")
    public PageList<Shop> queryByPage(@RequestBody  ShopQuery query) {
        return shopService.queryByPage(query);
    }
    // 批量删除

    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        shopService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 店铺入住
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto dto) {
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    /**
     * 店铺审核通过and驳回
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid ShopAuditDto dto) {
        if(dto.getCheckCode() == 1){
            shopService.auditPass(dto);
        }
        if(dto.getCheckCode() == 2) {
            shopService.reject(dto);
        }
        return AjaxResult.me().success();
    }

    /**
     * 店铺激活接口
     */
    @GetMapping("/activity/{id}")
    public AjaxResult activity(@PathVariable("id") Long id) {
        {
            shopService.activity(id);
            return AjaxResult.me().success();
        }
    }

    // 导出
    @GetMapping("/exprotExcel")
    public void exprotExcel(HttpServletResponse response) {
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all, "宠物之家店铺信息", "店铺信息", Shop.class, "店铺信息.xlsx", response);
    }

    // 导入
    @PutMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        List<Shop> importExcel = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        // 添加进数据库
        importExcel.forEach(System.out::println);
        for (Shop shop : importExcel) {
            shopService.add(shop);
        }
    }


}